package mk.jug.academy.controller.support;

import java.util.List;

import javax.servlet.jsp.JspWriter;

import mk.jug.academy.model.Sponsor;
import mk.jug.academy.service.SponsorService;

import org.springframework.web.servlet.tags.RequestContextAwareTag;

/**
 * Custom tag to render the sponsor section on each page.
 *
 * @author Ice.Penov
 */
public class SponsorsTag extends RequestContextAwareTag {

	private static final long serialVersionUID = 8908050019372085961L;

	@Override
	protected int doStartTagInternal() throws Exception {
		SponsorService sponsorService = getRequestContext().getWebApplicationContext().getBean(SponsorService.class);
		List<Sponsor> sponsors = sponsorService.listActive(1, 4).getItems();		
		JspWriter out = pageContext.getOut();
		if (sponsors.isEmpty()) {
			out.println("<div class=\"banner\">");
			out.println("<table width=\"200px\" height=\"50px\" style=\"text-align:center\"><tr>");
			out.println("<td valign=\"middle\"><a href=\"/register.do?type=sponsor\" title=\"Open the sponsor registration form\" class=\"plainLink\"><img src=\"/img/sponsor_support.jpg\" border=\"0\"/></a></td>");
			out.println("</tr></table></div>");
		} else {
			for (Sponsor sponsor: sponsors) {
				out.println("<div class=\"banner\">");
				out.println("<table width=\"200px\" height=\"50px\" style=\"text-align:center\"><tr>");
				out.println("<td valign=\"middle\"><a href=\"" + sponsor.getUrl() + "\" target=\"_blank\" title=\"" + sponsor.getDescription() + "\" class=\"plainLink\"><img src=\"/serve.do?key=" + sponsor.getImageKey() + "\" border=\"0\"/></a></td>");
				out.println("</tr></table></div>");
			}
		}
		return EVAL_PAGE;
	}

}